package com.wdzl.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.wdzl.dao.SchoolDao;
import com.wdzl.dao.StudentDao;
import com.wdzl.entity.Student;
import com.wdzl.util.DateUtil;



public class ManageForm extends JFrame {

	private ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField unameTextField;
	private JTextField emailTextField;
	private final JTextField regDate_textField;
	private JComboBox  comboBox;
	private JTextField phoneTextField;
	private JTextField ageField;
	private JTextField uidTextField;
	private JRadioButton manRadioButton;
	private JRadioButton nvRadioButton;
	private JButton button_mod;
	private StudentDao dao = new StudentDao();//数据库访问类

	/**
	 * Create the frame
	 */
	public ManageForm() {
		super();
		getContentPane().setLayout(null);
		setTitle("学员信息管理");
		setBounds(300, 260, 500, 407);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(Color.black, 1, false));
		panel_1.setBounds(10, 310, 473, 40);
		getContentPane().add(panel_1);

		final JButton button_4 = new JButton();
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				AddForm af = new AddForm();
				af.setVisible(true);
			}
		});
		button_4.setText("添加");
		panel_1.add(button_4);

		final JButton button_2 = new JButton();
		button_2.setInheritsPopupMenu(true);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				///////////////////////查询事件////////////////////////////////
				String sid = uidTextField.getText();
				if(sid==null || sid.trim().equals("")){
					JOptionPane.showMessageDialog(ManageForm.this, "请输入编号！");

					return;
				}

				Student student = dao.queryStudentById(Integer.parseInt(sid));
				if(student == null){
					JOptionPane.showMessageDialog(ManageForm.this, "查无此人！！");
					return;
				}
				//数据存在 显示到界面
				unameTextField.setText(student.getSname());
				ageField.setText(student.getSage()+"");
				emailTextField.setText(student.getEmail());
				phoneTextField.setText(student.getPhone());
				regDate_textField.setText(DateUtil.formateDate(student.getAddDate()));
				if(student.isSex()){
					manRadioButton.setSelected(true);
				}else{
					nvRadioButton.setSelected(true);
				}

				comboBox.setSelectedItem(student.getSchoolname());

				//修改按钮可用
				button_mod.setEnabled(true);
			}
		});
		button_2.setText("查询");
		panel_1.add(button_2);

		button_mod = new JButton();
		button_mod.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				//修改学员信息

				//1.获取数据 封装到实体类对象
				Student student = new Student();
				student.setSid(Integer.parseInt(uidTextField.getText()));
				student.setEmail(emailTextField.getText());
				student.setSage(Integer.parseInt(ageField.getText()));
				student.setPhone(phoneTextField.getText());
				student.setSchoolname(comboBox.getSelectedItem()+"");
				student.setSname(unameTextField.getText());

				if(manRadioButton.isSelected()){
					student.setSex(true);
				}else {
					student.setSex(false);
				}

				boolean is = dao.update(student);
				if(is){
					JOptionPane.showMessageDialog(ManageForm.this, "修改成功！");
				}else{
					JOptionPane.showMessageDialog(ManageForm.this, "修改失败！");
				}
			}
		});
		button_mod.setText("修改");
		button_mod.setEnabled(false);
		panel_1.add(button_mod);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {

				int flag = JOptionPane.showConfirmDialog(ManageForm.this, "您确定删除此学员信息么？");
				if(flag!=JOptionPane.OK_OPTION){
					return;
				}

				String sid = uidTextField.getText();
				if(sid==null || sid.trim().equals("")){
					JOptionPane.showMessageDialog(ManageForm.this, "请输入学员编号！");
					return;
				}

				boolean is = dao.delStudent(Integer.parseInt(sid));
				if(is){
					uidTextField.setText("");
					unameTextField.setText("");
					ageField.setText("");
					emailTextField.setText("");
					phoneTextField.setText("");
					regDate_textField.setText("");
					manRadioButton.setSelected(false);
					nvRadioButton.setSelected(false);
					comboBox.setSelectedIndex(0);

					JOptionPane.showMessageDialog(ManageForm.this, "删除成功！");
				}else{
					JOptionPane.showMessageDialog(ManageForm.this, "删除失败！");
				}
			}
		});
		button.setText("删除");
		panel_1.add(button);

		final JButton button_3 = new JButton();
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				int sel = JOptionPane.showConfirmDialog(ManageForm.this,"您确定退出系统么？");
				if(sel == JOptionPane.OK_OPTION){
					System.exit(0);
				}
			}
		});
		button_3.setText("退出");
		panel_1.add(button_3);

		final JButton button_5 = new JButton();
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				new SearchFrame().setVisible(true);
			}
		});
		button_5.setText("高级查询");
		panel_1.add(button_5);

		final JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.black, 1, false));
		panel.setLayout(null);
		panel.setBounds(11, 10, 472, 295);
		getContentPane().add(panel);

		final JLabel label = new JLabel();
		label.setText("学员编号：");
		label.setBounds(125, 12, 66, 18);
		panel.add(label);

		uidTextField = new JTextField();
		uidTextField.setBounds(197, 10, 153, 22);
		panel.add(uidTextField);

		final JLabel label_1 = new JLabel();
		label_1.setText("学员年龄：");
		label_1.setBounds(128, 81, 66, 18);
		panel.add(label_1);

		ageField = new JTextField();
		ageField.setBounds(200, 79, 150, 22);
		panel.add(ageField);

		final JLabel label_2 = new JLabel();
		label_2.setText("手机号码：");
		label_2.setBounds(127, 222, 66, 18);
		panel.add(label_2);

		phoneTextField = new JTextField();
		phoneTextField.setBounds(199, 220, 151, 22);
		panel.add(phoneTextField);

		final JLabel label_2_1 = new JLabel();
		label_2_1.setText("所属学校：");
		label_2_1.setBounds(127, 185, 66, 18);
		panel.add(label_2_1);

		comboBox = new JComboBox();
		SchoolDao dao = new SchoolDao();
		Vector<String> schoolnames = dao.querySchool();
		schoolnames.add(0, "--请选择--");
		comboBox.setModel(new DefaultComboBoxModel(schoolnames));

		comboBox.setBounds(199, 180, 151, 27);
		panel.add(comboBox);

		final JLabel label_3 = new JLabel();
		label_3.setText("添加日期：");
		label_3.setBounds(127, 252, 66, 18);
		panel.add(label_3);

		regDate_textField = new JTextField();
		regDate_textField.setEditable(false);
		regDate_textField.setBounds(199, 250, 151, 22);
		panel.add(regDate_textField);

		final JLabel label_2_2 = new JLabel();
		label_2_2.setText("电子邮箱：");
		label_2_2.setBounds(128, 147, 66, 18);
		panel.add(label_2_2);

		emailTextField = new JTextField();
		emailTextField.setBounds(200, 145, 150, 22);
		panel.add(emailTextField);

		unameTextField = new JTextField();
		unameTextField.setBounds(198, 42, 152, 22);
		panel.add(unameTextField);

		final JLabel label_5 = new JLabel();
		label_5.setText("学员名称：");
		label_5.setBounds(126, 44, 66, 18);
		panel.add(label_5);

		manRadioButton = new JRadioButton();
		buttonGroup.add(manRadioButton);
		manRadioButton.setText("男");
		manRadioButton.setBounds(197, 110, 38, 24);
		panel.add(manRadioButton);

		nvRadioButton = new JRadioButton();
		nvRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
			}
		});
		nvRadioButton.setText("女");
		buttonGroup.add(nvRadioButton);
		nvRadioButton.setBounds(255, 110, 48, 24);
		panel.add(nvRadioButton);

		final JLabel label_6 = new JLabel();
		label_6.setText("学员性别：");
		label_6.setBounds(127, 112, 66, 18);
		panel.add(label_6);

		final JLabel label_4 = new JLabel();
		label_4.setBounds(125, 355, 199, 18);
		getContentPane().add(label_4);
		label_4.setFont(new Font("", Font.BOLD | Font.ITALIC, 12));
		label_4.setText("当前版本：version1.0 @WDZL所有");
		//
	}

}
